package com.kwan80gmail.flextime.activities;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.kwan80gmail.flextime.R;
import com.kwan80gmail.flextime.models.Place;
import com.kwan80gmail.flextime.models.SqlLiteHelper;

public class PlaceSettingsActivity extends ActionBarActivity
{
    Button deleteButton;
    Button renameButton;
    SqlLiteHelper database;
    private Place place;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        database = new SqlLiteHelper(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_places_settings);

        //gets intent from locations
        Intent intent = getIntent();

        place = (Place) intent.getSerializableExtra("Place");

        final TextView locationName = (TextView) findViewById(R.id.locationName);
        locationName.setText(place.getName());

        TextView latitude = (TextView) findViewById(R.id.Latitude);
        latitude.setText(String.valueOf(place.getLatitude()));
        TextView longitude = (TextView) findViewById(R.id.Longitude);
        longitude.setText(String.valueOf(place.getLongitude()));

        deleteButton = (Button) findViewById(R.id.DeleteBtn);
        renameButton = (Button)findViewById(R.id.renameBtn);

        deleteButton.setOnLongClickListener(new View.OnLongClickListener()
        {
            @Override
            public boolean onLongClick(View v)
            {
                database.deletePlaceByName(place.getlocationId());
                Toast.makeText(getApplicationContext(), "Place Deleted", Toast.LENGTH_LONG).show();
                return false;
            }
        });

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("Rename Place");
        final EditText input = new EditText(this);
        input.setInputType(InputType.TYPE_CLASS_TEXT);
        builder.setView(input);

        renameButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                builder.setPositiveButton("Save", new DialogInterface.OnClickListener()
                {

                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        database.updatePlaceName(input.getText().toString(), place.getlocationId());
                        Toast.makeText(getApplicationContext(), "New Place Name " + input.getText().toString(), Toast.LENGTH_LONG).show();
                    }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                {
                    @Override
                    public void onClick(DialogInterface dialog, int which)
                    {
                        dialog.cancel();
                    }
                });

                builder.show();
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_location_settings, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings)
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
